<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList()">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage()">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" show-overflow-tooltip label="商品名称"></el-table-column>
        <el-table-column width="90" prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column width="90" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column width="180" prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time}}
          </template>
        </el-table-column>
        <el-table-column width="130px" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="openEditDialog(scope.row)"></el-button>
            <el-button size="mini" type="danger" icon="el-icon-remove" @click="removeById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!--编辑商品对话框-->
    <el-dialog title="编辑商品信息" :visible.sync="editDialogVisible" width="50%" @close="closeEditDialog()">
      <el-form :model="editForm" ref="editFormRef" label-width="80px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script lang="ts">
interface goodsProp {
  add_time?: number;
  cat_id?: number;
  cat_one_id?: number;
  cat_three_id?: number;
  cat_two_id?: number;
  goods_id: number;
  goods_name: string;
  goods_number: number;
  goods_price: number;
  goods_state?: number;
  goods_weight?: number;
  hot_mumber?: number;
  is_promote?: boolean;
  upd_time?: number;
}
import { goodList, updateGood, delGoods } from "@/request/api";
import Vue from "vue";
import Component from "vue-class-component";

@Component
export default class Goods extends Vue {
  goodsList: [] = [];
  queryInfo: {
    query: string;
    pagenum: number;
    pagesize: number;
  } = { query: "", pagenum: 1, pagesize: 10 };
  total: number = 0;
  editDialogVisible: boolean = false;
  editForm: goodsProp = {
    goods_id: 0,
    goods_name: "",
    goods_price: 0,
    goods_number: 0,
  };
  created() {
    this.getGoodsList();
  }
  // 获取商品列表
  async getGoodsList() {
    const { data, meta } = await goodList(this.queryInfo);
    if (meta.status !== 200) {
      return this.$message.error("获取商品列表失败");
    }
    this.goodsList = data.goods;
    this.total = data.total;
  }
  // 监听每页数量变化触发
  handleSizeChange(newSize: number) {
    this.queryInfo.pagesize = newSize;
    this.getGoodsList();
  }
  // 监听当前页改变
  handleCurrentChange(newPagenum: number) {
    this.queryInfo.pagenum = newPagenum;
    this.getGoodsList();
  }
  // 打开编辑对话框
  openEditDialog(Goods: goodsProp) {
    console.log(Goods);
    this.editForm = Goods;
    console.log(this.editForm);
    this.editDialogVisible = true;
  }
  // 关闭编辑对话框
  closeEditDialog() {}
  // 保存编辑商品
  async editGoods() {
    console.log(this.editForm);
    const { data, meta } = await updateGood(this.editForm.goods_id, {
      goods_name: this.editForm.goods_name,
      goods_price: this.editForm.goods_price,
      goods_number: this.editForm.goods_number,
    });
  }
  // 删除单个商品
  async removeById(id: number) {
    const confirmRes = await this.$confirm(
      "此操作将永久删除该商品, 是否继续?",
      "提示",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }
    ).catch((err) => {
      return err;
    });
    if (confirmRes !== "confirm")
      return this.$message.info("你取消了删除商品的操作");
    const { data, meta } = await delGoods(id);
    if (meta.status !== 200) return this.$message.error("删除数据失败");
    this.$message.success("删除数据成功");
    this.getGoodsList();
  }
  // 点击添加商品跳转到添加页面
  goAddpage() {
    this.$router.push("/add");
  }
}
</script>

<style lang="less" scoped>
</style>